import Vue from 'vue';
import axios from 'axios';
Vue.prototype.axios = axios;
// 创建axios实例
const $axios = axios.create();
$axios.defaults.timeout = 1000 * 50; // 请求超时时间

// request拦截器
$axios.interceptors.request.use(config => {
  if (sessionStorage.getItem('token')) {
    config.headers.Authorization = sessionStorage.getItem('token')
  }
  if (navigator.userAgent.indexOf('Trident') != -1) {
    if (config.method == 'post') {
      config.data = {
        ...config.data,
        t: Date.now()
      };
    } else if (config.method == 'get') {
      config.params = {
        ...config.params,
        t: Date.now()
      };
    }
  }
  return config;
}, error => {
  return Promise.reject(error);
});

// respone 拦截器
$axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    return Promise.reject(error);
  }
);

export default $axios;
